package geektime.spring.springbucks.waiter.handler;

import geektime.spring.springbucks.waiter.model.OrderState;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class OrderStateTypeHandler extends BaseTypeHandler<OrderState> {


    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, OrderState orderState, JdbcType jdbcType) throws SQLException {
        preparedStatement.setInt(i,orderState.ordinal());
    }

    @Override
    public OrderState getNullableResult(ResultSet resultSet, String columnName) throws SQLException {
        return parseOrderState(resultSet.getInt(columnName));
    }

    @Override
    public OrderState getNullableResult(ResultSet resultSet, int i) throws SQLException {
        return parseOrderState(resultSet.getInt(i));
    }

    @Override
    public OrderState getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        return parseOrderState(callableStatement.getInt(i));
    }

    private OrderState parseOrderState(int ordinal){
        return OrderState.values()[ordinal];
    }


}
